package com.fingard.dsp.bank.directbank.bos01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class BOS01Balance extends BOS01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();
        
        try {
            BOS01Header tmpHeader = new BOS01Header();
            tmpHeader.opName = "batchQueryBalanceCurrent1_1Op";
            tmpHeader.serialNo = getQryTransID();
            tmpHeader.needSigned = false;
            
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, tmpHeader);
            
            int queryCount = 0;
            XElement tmpXElement = new XElement("opReqSet", "");
            while (p_req7506.readRecord()) {
                boolean jionforeignAct = getBankConfig().getValueAsBool("jionforeignAct");
                WriteBankLogLn("jionforeignAct:" + jionforeignAct);
                if (jionforeignAct) {
                    String foreignActMapping = getBankConfig().getMapValueByKey("foreignActMapping", p_req7506.readedDetail.actNum);
                    WriteBankLogLn("foreignActMapping:" + "[key:" + p_req7506.readedDetail.actNum + ",value:" + foreignActMapping + "]");
                    p_req7506.readedDetail.actNum = StringHelper.joinNotEmpty("", foreignActMapping, p_req7506.readedDetail.actNum);
                }

                queryCount++;
                XElement opRequestXE = new XElement("opRequest", "");
                opRequestXE.addChild(new XElement("ACNO", p_req7506.readedDetail.actNum));
                tmpXElement.addChild(opRequestXE);
            }
            xmlWriter.writeElementString("SUMU", String.valueOf(queryCount));
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();
            
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), tmpHeader);
            
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstText("/BOSEBankData/opRep/retCode");
                    String tmprspmsg = tmpBkReader.getFirstText("/BOSEBankData/opRep/errMsg");
                    if ("0".equals(tmprspcod)) {
                        while (tmpBkReader.read("/BOSEBankData/opRep/opResultSet/opResult")) {
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("ACNO");
                            tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("YUER");
                            tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("KYER");
                            
                            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmprspmsg);
                    }
                    if (tmprspmsg.contains("非法SESSION") || tmprspmsg.contains("SESSION已超时")) {
                        bankFront.token = "";
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpStrRet[1]);
            }
        } catch (Exception ex) {
        	ex.printStackTrace();
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(ex.getMessage());
            WriteBankLogLn(ex);
        }
        
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
        retAts.writeStart();
        try {
            BOS01Header tmpHeader = new BOS01Header();
            tmpHeader.opName = "queryHistoryCurrentBalance1_1Op";
            tmpHeader.needSigned = false;
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7505.getDateFrom());
            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7505.getDateTo());
            StringBuilder messageStr = new StringBuilder();
            List<RetAtsHisBalDetail> retAtsHisBalDetailList =new ArrayList<RetAtsHisBalDetail>();
            while (tmpCalendarTo.compareTo(tmpCalendarFrom) != -1) {
                try {
                    XmlTextWriter xmlWriter = new XmlTextWriter();
                    tmpHeader.serialNo = getQryTransID();
                    writeCommonHeader(xmlWriter, tmpHeader);
                    boolean jionforeignAct = getBankConfig().getValueAsBool("jionforeignAct");
                    WriteBankLogLn("jionforeignAct:" + jionforeignAct);
                    if (jionforeignAct) {
                        String foreignActMapping = getBankConfig().getMapValueByKey("foreignActMapping", p_req7505.actNum);
                        WriteBankLogLn("foreignActMapping:" + "[key:" + p_req7505.actNum + ",value:" + foreignActMapping + "]");
                        p_req7505.actNum = StringHelper.joinNotEmpty("", foreignActMapping, p_req7505.actNum);
                    }
                    xmlWriter.writeElementString("ACNO", p_req7505.actNum);
                    xmlWriter.writeElementString("DATE", Format.Date8Format.format(tmpCalendarFrom.getTime()));
                    xmlWriter.writeEndDocument();
                    String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), tmpHeader);
                    if (tmpStrRet[0].length() == 0) {
                        StringBuilder tmpSbMsg = new StringBuilder();
                        XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                        if (tmpBkReader != null) {
                            String tmprspcod = tmpBkReader.getFirstText("/BOSEBankData/opRep/retCode");
                            String tmprspmsg = tmpBkReader.getFirstText("/BOSEBankData/opRep/errMsg");
                            if ("0".equals(tmprspcod)) {
                                while (tmpBkReader.read("/BOSEBankData/opRep/opResult")) {
                                    RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
                                    tmpRetDetail.balDate = Format.Date10Format.format(tmpCalendarFrom.getTime());//余额日期
                                    tmpRetDetail.actBal = tmpBkReader.readedXElement.getSingleText("AVLB");//历史余额
                                    retAtsHisBalDetailList.add(tmpRetDetail);
                                }
                            } else {
                                messageStr.append(Format.Date10Format.format(tmpCalendarFrom.getTime())).append(":").append(tmprspmsg).append(";");
                            }
                            if (tmprspmsg.contains("非法SESSION") || tmprspmsg.contains("SESSION已超时")) {
                                bankFront.token = "";
                            }
                        } else {
                            messageStr.append(Format.Date10Format.format(tmpCalendarFrom.getTime())).append(":").append(tmpSbMsg).append(";");
                        }
                    } else {
                        messageStr.append(Format.Date10Format.format(tmpCalendarFrom.getTime())).append(":").append(tmpStrRet[1]).append(";");
                    }
                    tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
                } catch (Exception e) {
                    messageStr.append(Format.Date10Format.format(tmpCalendarFrom.getTime())).append(":").append(e.getMessage()).append(";");
                }
            }
            if (retAtsHisBalDetailList.size() > 0) {
                retAts.writeRespCode("0");
                if (messageStr.toString().length() > 0) {
                    retAts.writeRespInfo(messageStr.toString());
                }
                for (RetAtsHisBalDetail tmpRetAtsHisBalDetail : retAtsHisBalDetailList) {
                    retAts.writeRetDetail(tmpRetAtsHisBalDetail);
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(messageStr.toString());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(ex.getMessage());
            WriteBankLogLn(ex);
        } finally {
            retAts.writeEnd();
        }
        return retAts;
    }
}
